/**
 * controller_shopcart.js
 * 购物车控制器
 * 
 * @author 	王中艺	<wangzy_smile@qq.com>
 * @date 	2017-02-23
 */

//购物车数据列表
 app.controller('ShopCartList', function($scope, $location, $http, common){
 	$scope.list 	= new Array();
 	$scope.pageHtml = '';
 	$scope.nPage 	= 1;
 	$scope.loading 	= false;

 	//加载数据
 	$scope.load_data 	= function(page){
 		if(!page)	page = 1;
 		var url 	= config.prefix_url+'/ShopCart/getList?p='+page;
 		$http.get(url).success(function(info){
 			console.log(info);
 			$scope.loading 	= false;
 			if(info.code != 1){
 				common.dealError(info);
 			}else{
 				$scope.nPage 	= page;
 				$scope.list 	= info.data.list;
 				$scope.pageHtml = info.data.page_html;
 				common.startPage($scope.load_data);
 			}
 		})
 		$scope.loading 	= true;
 	}

 	//详情信息显示
 	$scope.detail 	= function(goods_infos){
 		$scope.goodsInfos 	= goods_infos;
 		$('.modal.goods-infos').modal('show');
 	}

 	$scope.load_data(1);
 });

//购物车报表控制器
app.controller('ShopCartReport', function($scope, $location, $http, common){
 	$scope.dateS 	= '';
 	$scope.dateE 	= '';
 	$scope.chart 	= {}
 	$scope.loading 	= false;

 	//初始化js
 	$scope.init_js 	= function(){
 		$('.input-date').datetimepicker({
	        language:  'zh-CN',
	        format: 'yyyy-mm-dd',
	        weekStart: 1,
	        todayBtn:  1,
	        autoclose: 1,
	        todayHighlight: 1,
	        startView: 2,
	        forceParse: 0,
	        showMeridian: 1,
	        minView	: 2
	    });

	    //回车键监听
	    common.listenEnterKey(function(){
	        $scope.load_data();
	    });
 	}
 	//加载数据
 	$scope.load_data 	= function(){
 		var url 	= config.prefix_url+'/ShopCart/getReport?'+
 			'&date_start='+$scope.dateS+
 			'&date_end='+$scope.dateE;

 		$http.get(url).success(function(info){
 			$scope.loading 	= false;
 			if(info.code != 1){
 				common.dealError(info);
 			}else{
 				var data 	= info.data;
 				var xAxis 	= new Array();
 				var dataA 	= new Array();
 				var dataC 	= new Array();
 				for(var k in data){
 					xAxis.push(data[k].date);
 					dataA.push(parseFloat(data[k].amount));
 					dataC.push(parseInt(data[k].count));
 				}
				var seriesA 	= [{
					name : '销售金额',
					data : dataA
				}]
				var seriesC 	= [{
					name : '销售数量',
					data : dataC
				}]
 				common.makeChart($('.chart-amount'), '购物车销售金额', xAxis, '销售金额(元)', seriesA);
 				common.makeChart($('.chart-count'), '购物车销售数量', xAxis, '销售数量(个)', seriesC);
 			}
 		})
 		$scope.loading 	= true;
 	}

 	$scope.load_data();
 	$scope.init_js();
})

//购物车交易记录
app.controller('ShopCartBargainList', function($scope, $location, $http, common){
	$scope.list 	= {};
	$scope.loading 	= false;
	$scope.nPage 	= 1;
	$scope.pageHtml 	= '';
	$scope.keyword 	= '';
	$scope.dateStart 	= '';
	$scope.dateEnd 	= '';
	$scope.total 	= {};

	//初始化JS
	$scope.init_js 	= function(){
		$('.input-date').datetimepicker({
	        language:  'zh-CN',
	        format: 'yyyy-mm-dd',
	        weekStart: 1,
	        todayBtn:  1,
	        autoclose: 1,
	        todayHighlight: 1,
	        startView: 2,
	        forceParse: 0,
	        showMeridian: 1,
	        minView	: 2
	    });

	    //回车键监听
	    common.listenEnterKey(function(){
	        $scope.load_data(1);
	    });
	}

	//加载数据
	$scope.load_data 	= function(page){
		var url 	= config.prefix_url+'/ShopCart/getBargainList?'+
			'&p='+page+
			'&date_start='+$scope.dateStart+
			'&date_end='+$scope.dateEnd+
			'&keyword='+$scope.keyword;
		$http.get(url).success(function(info){
			console.log(info);
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
				return false;
			}
			$scope.nPage 	= page;
			$scope.loading 	= false;
			$scope.list 	= info.data.list;
			$scope.pageHtml 	= info.data.page_html;
			$scope.total 	= info.data.total;
			common.startPage($scope.load_data);
		});
		$scope.loading 	= true;
	}

	//详情
	$scope.detail 	= function(item){
		$scope.nItemInfo 	= item;
		var url 	= config.prefix_url+'/ShopCart/detailBargain?shopcart_id='+item.id;
		$http.get(url).success(function(info){
			console.log(info);
			if(info.code != 1){
				common.dealError(info);
				return false;
			}
			$scope.nMoneyInfo 	= info.data.money_info;
			$scope.nDeductList 	= info.data.deduct_list;
			$scope.loading 	= false;
			$('.modal.bargain-detail').modal('show');
		});
		$scope.loading 	= true;
	}

	//导出excel
	$scope.out_excel 	= function(){
		var url 	= config.prefix_url + '/ShopCart/outBargainExcel?'+
			'&keyword='+$scope.keyword+
			'&date_start='+$scope.dateStart+
			'&date_end='+$scope.dateEnd;

		$http.get(url).success(function(info){
			if(info.code != 1){
				common.dealError(info);
				return false;
			}

			var count 	= info.data.count;
			var length 	= 1000;
			var start 	= 0;
			while(start < count){
				var outUrl 	= url + '&start='+start+'&length='+length;
				window.open(outUrl, '_blank');
				start 	+= length;
			}
		})
	}

	$scope.load_data(1);
	$scope.init_js();
})